package com.lvtongka.app.dialog.adapter

import android.annotation.SuppressLint
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.lvtongka.app.R
import com.lvtongka.app.common.AppUtils
import com.lvtongka.app.databinding.DialogSelectCouponItemBinding
import com.lvtongka.app.ui.coupon.bean.CouponItem

class CouponSelectAdapter(private var itemSelectListener: DialogSelectListener) :
    RecyclerView.Adapter<CouponSelectAdapter.CouponSelectHolder>() {
    private var mList: MutableList<CouponItem> = mutableListOf()

    @SuppressLint("NotifyDataSetChanged")
    fun setList(list: List<CouponItem>) {
        mList.clear()
        mList.addAll(list)
        notifyDataSetChanged()
    }

    open inner class CouponSelectHolder(val binding: DialogSelectCouponItemBinding) : RecyclerView.ViewHolder(binding.root)

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CouponSelectHolder {
        val binding = DialogSelectCouponItemBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        return CouponSelectHolder(binding)
    }

    @SuppressLint("SetTextI18n")
    override fun onBindViewHolder(holder: CouponSelectHolder, position: Int) {
        val item = mList[position]
        holder.binding.tvCouponItemTime.text = "有效期" + item.valid
        holder.binding.tvCouponItemBalance.text = AppUtils.convertFenToYuan(item.balanceCent)
        val colorId = selectColor(item.type)
        if (!TextUtils.isEmpty(item.kinNote)) {
            holder.binding.tvCouponItemName.text = item.typeValue
            holder.binding.tvQsq.visibility = View.VISIBLE
            holder.binding.tvQsq.setTextColor(holder.binding.tvQsq.resources.getColor(colorId))
        } else {
            holder.binding.tvCouponItemName.text = item.typeValue + "("+ item.couponNo.substring(item.couponNo.length-4) +")"
            holder.binding.tvQsq.visibility = View.GONE
        }
        holder.binding.tvCouponItemName.setTextColor(holder.binding.tvCouponItemName.resources.getColor(colorId))
        holder.binding.tvCouponItemTime.setTextColor(holder.binding.tvCouponItemName.resources.getColor(colorId))
        holder.binding.tvCouponItemDesc.setTextColor(holder.binding.tvCouponItemName.resources.getColor(colorId))
        holder.binding.ivDown.setImageResource(selectDrawable(item.type))
        holder.binding.rlRoot.setBackgroundResource(selectBg(item.type))
        holder.binding.rlRoot.setOnClickListener {
            itemSelectListener.onItemClick(item)
        }
    }

    override fun getItemCount(): Int {
        return mList.size
    }

    private fun selectBg(n: String) : Int {
        return when (n) {
            "A" -> R.mipmap.icon_coupon_jiri
            "B" -> R.mipmap.icon_coupon_shengri
            "C" -> R.mipmap.icon_coupon_shuying
            "D" -> R.mipmap.icon_coupon_laobao
            "E" -> R.mipmap.icon_coupon_zhunong
            else -> R.mipmap.icon_coupon_jiri
        }
    }

    private fun selectColor(n: String) : Int {
        return when (n) {
            "A" -> R.color.textAC2C45
            "B" -> R.color.textBD532A
            "C" -> R.color.text755919
            "D" -> R.color.text645DBF
            "E" -> R.color.text488A56
            else -> R.color.white
        }
    }

    private fun selectDrawable(n: String) : Int {
        return when (n) {
            "A" -> R.mipmap.icon_coupon_d_cheng
            "B" -> R.mipmap.icon_coupon_d_fen
            "C" -> R.mipmap.icon_coupon_d_zong
            "D" -> R.mipmap.icon_coupon_d_zi
            "E" -> R.mipmap.icon_coupon_d_lv
            else -> R.mipmap.icon_coupon_d_cheng
        }
    }
}

interface DialogSelectListener {
    fun onItemClick(item: CouponItem)
}